using System;
using System.Collections.Generic;
using System.Windows.Forms;
using SqlCeViewer.CommonMethod;
using SqlCeViewer.MainForm;
using SqlCeViewer.Base;

namespace SqlCeViewer
{
    static class Program
    {

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

            
#if DEBUG
            GlobalInfo.log.LogLineMsg("Start App");
#else

#endif


#if DEBUG
            GlobalInfo.log.LogLineMsg("Start Authentication");
#else

#endif
            //NativeGate.Util u = new NativeGate.Util();
            //if (!u.LicenseKeySimplestValidation(Properties.Settings.Default.SQLCELicense))
            //{
            //    MessageBox.Show("License key is invalid ");
            //    return;
            //}

#if DEBUG
            GlobalInfo.log.LogLineMsg("Start SingleInstance ");
#else

#endif

            if (System.Windows.Forms.ApplicationUtility.IsAlreadyRunning())
            {
                System.Windows.Forms.ApplicationUtility.BringOtherInstanceToFront();
            }
            else
            {


#if DEBUG
                GlobalInfo.log.LogLineMsg("Count Run times");
#else

#endif

                int runTimes = Properties.Settings.Default.RumTimes;
                runTimes++;
                Properties.Settings.Default["RumTimes"] = runTimes;
                Properties.Settings.Default.Save();
                
#if DEBUG
                GlobalInfo.log.LogLineMsg("Create Enginer Successful then Loading Main UI ");
#else

#endif


                Application.Run(new NewLoginForm());
            }


        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {

            GlobalInfo.log.LogLineMsg(e.Exception.Message);
            XLCS.Common.ProcessException.DisplayErrors(e.Exception);
        }
    }
}